package jprint.jfx.http;

import javax.annotation.Nonnull;
import jprint.base.AppContext;
import jprint.base.http.AbstractRequestHandler;
import jprint.base.http.Http;

/**
 * @author zwz
 * Created on 2020-11-21
 */
public class ApiHandler extends AbstractRequestHandler {

    private Http.HttpRequestHandler[] subHandlers;

    public ApiHandler(@Nonnull AppContext appContext) {
        subHandlers = new Http.HttpRequestHandler[] {
            new DesignHandler(appContext),
            new FontHandler(appContext),
            new LabelHandler(appContext),
            new PrintHandler(appContext),
            new VersionHandler(),
        };
    }

    @Override public boolean handle(Http.HttpRequest request, Http.HttpResponse response) {
        if (!request.uri().startsWith("/api/")) return false;
        for (Http.HttpRequestHandler h : subHandlers) {
            if (h.handle(request, response)) {
                return true;
            }
        }
        return super.handle(request, response);
    }
}
